home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / disk / diskHeader.c.1.8 < prev    next >
Encoding:
Text File  |  1990-02-21  |  8.1 KB  |  294 lines

  1. /* 
  2.  * diskHeader.c --
  3.  *
  4.  *    Routines to read in the disk header information.
  5.  *
  6.  * Copyright (C) 1987 Regents of the University of California
  7.  * All rights reserved.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.8 90/01/31 17:05:19 jhh Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21. #include "diskUtils.h"
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. /*
  27.  * BOOT_SECTOR        Where the boot sectors start on disk.
  28.  */
  29. #define    BOOT_SECTOR        1
  30. #define NUM_BOOT_SECTORS    15
  31.  
  32. /*
  33.  *----------------------------------------------------------------------
  34.  *
  35.  * Disk_ReadDiskInfo--
  36.  *
  37.  *    Read the first sector and determine the basic disk info.
  38.  *
  39.  * Results:
  40.  *    A pointer to Disk_Info for the disk if could read it, 0
  41.  *    otherwise.
  42.  *
  43.  * Side effects:
  44.  *    Memory allocation.
  45.  *
  46.  *----------------------------------------------------------------------
  47.  */
  48. Disk_Info *
  49. Disk_ReadDiskInfo(fileID, partition)
  50.     int fileID;            /* Handle on raw disk */
  51.     int partition;        /* Index of the partition to format */
  52. {
  53.     Sun_DiskLabel    *sunLabelPtr;
  54.     Disk_Info        *diskInfoPtr;
  55.     char         buffer[DEV_BYTES_PER_SECTOR * FSDM_NUM_DOMAIN_SECTORS];
  56.     Fsdm_DomainHeader    *domainHeaderPtr = (Fsdm_DomainHeader *) buffer;
  57.     int            i;
  58.  
  59.     diskInfoPtr = (Disk_Info *)malloc(sizeof(Disk_Info));
  60.  
  61.     sunLabelPtr = Disk_ReadSunLabel(fileID);
  62.     if (sunLabelPtr != (Sun_DiskLabel *)0) {
  63.     Sun_DiskMap *sunMapPtr;
  64.     /*
  65.      * First sector looks like a sun label.
  66.      */
  67.     sunMapPtr = &sunLabelPtr->map[partition];
  68.     (void)strcpy(diskInfoPtr->asciiLabel, sunLabelPtr->asciiLabel);
  69.     diskInfoPtr->bootSector = BOOT_SECTOR;
  70.     diskInfoPtr->numDomainSectors = FSDM_NUM_DOMAIN_SECTORS;
  71.     diskInfoPtr->firstCylinder = sunMapPtr->cylinder;
  72.     diskInfoPtr->numCylinders = sunMapPtr->numBlocks /
  73.         (sunLabelPtr->numHeads * sunLabelPtr->numSectors);
  74.     diskInfoPtr->numHeads = sunLabelPtr->numHeads;
  75.     diskInfoPtr->numSectors = sunLabelPtr->numSectors;
  76.     for (i = BOOT_SECTOR + 1; 
  77.          i < FSDM_MAX_BOOT_SECTORS + 3; 
  78.          i+= FSDM_BOOT_SECTOR_INC) {
  79.         if (Disk_SectorRead(fileID, i, FSDM_NUM_DOMAIN_SECTORS, buffer) < 0) {
  80.         fprintf(stderr, "Can't read sector %d.\n", i);
  81.         return((Disk_Info *)0);
  82.         }
  83.         if (domainHeaderPtr->magic == FSDM_DOMAIN_MAGIC) {
  84.         diskInfoPtr->summarySector = i - 1;
  85.         diskInfoPtr->domainSector = i;
  86.         diskInfoPtr->numBootSectors = diskInfoPtr->summarySector - 1;
  87.         break;
  88.         }
  89.     }
  90.     if (i >= FSDM_MAX_BOOT_SECTORS + 3) {
  91.         diskInfoPtr->summarySector = -1;
  92.         diskInfoPtr->domainSector = -1;
  93.         diskInfoPtr->numBootSectors = -1;
  94.     }
  95.     } else {
  96.     Fsdm_DiskHeader *diskHeaderPtr;
  97.     Fsdm_DiskPartition *mapPtr;
  98.  
  99.     /*
  100.      * Not a sun label, try a sprite label.
  101.      */
  102.     diskHeaderPtr = Disk_ReadDiskHeader(fileID);
  103.     if (diskHeaderPtr == (Fsdm_DiskHeader *)0) {
  104.         fprintf(stderr, "Neither Sun nor Sprite label\n");
  105.         return((Disk_Info *)0);
  106.     }
  107.     mapPtr = &diskHeaderPtr->map[partition];
  108.     (void)strcpy(diskInfoPtr->asciiLabel, diskHeaderPtr->asciiLabel);
  109.     diskInfoPtr->bootSector = diskHeaderPtr->bootSector;
  110.     diskInfoPtr->numBootSectors = diskHeaderPtr->numBootSectors;
  111.     diskInfoPtr->summarySector = diskHeaderPtr->summarySector;
  112.     diskInfoPtr->domainSector = diskHeaderPtr->domainSector;
  113.     diskInfoPtr->numDomainSectors = diskHeaderPtr->numDomainSectors;
  114.     diskInfoPtr->firstCylinder = mapPtr->firstCylinder;
  115.     diskInfoPtr->numCylinders = mapPtr->numCylinders;
  116.     diskInfoPtr->numHeads = diskHeaderPtr->numHeads;
  117.     diskInfoPtr->numSectors = diskHeaderPtr->numSectors;
  118.     }
  119.     return(diskInfoPtr);
  120. }
  121.  
  122.  
  123.  
  124. /*
  125.  *----------------------------------------------------------------------
  126.  *
  127.  * Disk_ReadSunLabel --
  128.  *
  129.  *    Read the first sector of a disk partition and see if its
  130.  *    a sun label.  If so, return a pointer to a Sun_DiskLabel.
  131.  *
  132.  * Results:
  133.  *    A pointer to the super block data if could read it, 0 otherwise.
  134.  *
  135.  * Side effects:
  136.  *    Memory allocation.
  137.  *
  138.  *----------------------------------------------------------------------
  139.  */
  140. Sun_DiskLabel *
  141. Disk_ReadSunLabel(fileID)
  142.     int fileID;    /* Handle on raw disk */
  143. {
  144.     Address        buffer;
  145.     Sun_DiskLabel    *sunLabelPtr;
  146.  
  147.     buffer = (Address)malloc(DEV_BYTES_PER_SECTOR);
  148.  
  149.     if (Disk_SectorRead(fileID, 0, 1, buffer) < 0) {
  150.     return((Sun_DiskLabel *)0);
  151.     } else {
  152.     sunLabelPtr = (Sun_DiskLabel *)buffer;
  153.     if (sunLabelPtr->magic != SUN_DISK_MAGIC) {
  154.         return((Sun_DiskLabel *)0);
  155.     } else {
  156.         return(sunLabelPtr);
  157.     }
  158.     }
  159. }
  160.  
  161. /*
  162.  *----------------------------------------------------------------------
  163.  *
  164.  * Disk_ReadDiskHeader --
  165.  *    Read the super block and return a pointer to it.
  166.  *
  167.  * Results:
  168.  *    A pointer to the super block data if could read it, 0 otherwise.
  169.  *
  170.  * Side effects:
  171.  *    Memory allocation.
  172.  *
  173.  *----------------------------------------------------------------------
  174.  */
  175. Fsdm_DiskHeader *
  176. Disk_ReadDiskHeader(openFileID)
  177.     int openFileID;    /* Handle on raw disk */
  178. {
  179.     Address        buffer;
  180.     Fsdm_DiskHeader    *diskHeaderPtr;
  181.  
  182.     buffer = (Address) malloc(DEV_BYTES_PER_SECTOR);
  183.  
  184.     if (Disk_SectorRead(openFileID, 0, 1, buffer) < 0) {
  185.     return((Fsdm_DiskHeader *)0);
  186.     } else {
  187.     diskHeaderPtr = (Fsdm_DiskHeader *)buffer;
  188.     if (diskHeaderPtr->magic != FSDM_DISK_MAGIC) {
  189.         return((Fsdm_DiskHeader *)0);
  190.     } else {
  191.         return(diskHeaderPtr);
  192.     }
  193.     }
  194. }
  195.  
  196. /*
  197.  *----------------------------------------------------------------------
  198.  *
  199.  * Disk_ReadDomainHeader --
  200.  *    Read the domain header and return a pointer to it.
  201.  *
  202.  * Results:
  203.  *    A pointer to the domain header if could read it, NULL otherwise.
  204.  *
  205.  * Side effects:
  206.  *    Memory allocation.
  207.  *
  208.  *----------------------------------------------------------------------
  209.  */
  210. Fsdm_DomainHeader *
  211. Disk_ReadDomainHeader(fileID, diskInfoPtr)
  212.     int fileID;            /* Stream to raw disk */
  213.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  214. {
  215.     Fsdm_DomainHeader    *headerPtr;
  216.  
  217.     headerPtr = (Fsdm_DomainHeader *)malloc(diskInfoPtr->numDomainSectors *
  218.                      DEV_BYTES_PER_SECTOR);
  219.  
  220.     if (Disk_SectorRead(fileID, diskInfoPtr->domainSector,
  221.                     diskInfoPtr->numDomainSectors,
  222.                     (Address)headerPtr) < 0) {
  223.     return((Fsdm_DomainHeader *)0);
  224.     } else {
  225.     if (headerPtr->magic != FSDM_DOMAIN_MAGIC) {
  226.         fprintf(stderr, "Disk_ReadDomainHeader, bad magic <%x>\n",
  227.             headerPtr->magic);
  228.         free((Address)headerPtr);
  229.         return((Fsdm_DomainHeader *)0);
  230.     } else {
  231.         return(headerPtr);
  232.     }
  233.     }
  234. }
  235.  
  236. /*
  237.  *----------------------------------------------------------------------
  238.  *
  239.  * Disk_ReadSummaryInfo --
  240.  *
  241.  *    Read the summary information and return a pointer to it.
  242.  *
  243.  * Results:
  244.  *    A pointer to the summary information if it could be read,
  245.  *    NULL otherwise.
  246.  *
  247.  * Side effects:
  248.  *    Memory allocation.
  249.  *
  250.  *----------------------------------------------------------------------
  251.  */
  252. Fsdm_SummaryInfo *
  253. Disk_ReadSummaryInfo(fileID, diskInfoPtr)
  254.     int fileID;            /* Stream to raw disk */
  255.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  256. {
  257.     Fsdm_SummaryInfo *summaryPtr;
  258.  
  259.     summaryPtr = (Fsdm_SummaryInfo *) malloc (sizeof(Fsdm_SummaryInfo));
  260.  
  261.     if (Disk_SectorRead(fileID, diskInfoPtr->summarySector, 1,
  262.                     (Address)summaryPtr) < 0) {
  263.     return((Fsdm_SummaryInfo *)0);
  264.     } else {
  265.     return(summaryPtr);
  266.     }
  267. }
  268.  
  269. /*
  270.  *----------------------------------------------------------------------
  271.  *
  272.  * Disk_WriteSummaryInfo --
  273.  *
  274.  *    Write the summary information.
  275.  *
  276.  * Results:
  277.  *    0 if write succeeded, -1 otherwise
  278.  *
  279.  * Side effects:
  280.  *    The summary information is written to the disk.
  281.  *
  282.  *----------------------------------------------------------------------
  283.  */
  284. int
  285. Disk_WriteSummaryInfo(fileID, diskInfoPtr, summaryPtr)
  286.     int fileID;            /* Stream to raw disk */
  287.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  288.     Fsdm_SummaryInfo *summaryPtr; /* Summary information */
  289. {
  290.  
  291.     return Disk_SectorWrite(fileID, diskInfoPtr->summarySector, 1,
  292.                     (Address)summaryPtr);
  293. }
  294.